package com.qf.smsplatform.webmaster.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.webmaster.dto.R;
import com.qf.smsplatform.webmaster.pojo.TAdminUser;
import com.qf.smsplatform.webmaster.pojo.TDirtyword;
import com.qf.smsplatform.webmaster.service.DirtywordService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dirty")
@Api(tags = "敏感词管理")
public class DirtywordController {


    private DirtywordService dirtywordService;

    @Autowired
    public void setDirtywordService(DirtywordService dirtywordService){
        this.dirtywordService = dirtywordService;
    }

    @GetMapping("/dirty/{id}")
    public R findDirtyById(@PathVariable Long id){
        TDirtyword dirtyById = dirtywordService.findDirtyById(id);
        return R.setOk(dirtyById);

    }

    @PostMapping("/dirty/{dirtyword}")
    public R addDirtyword(@PathVariable String dirtyword, HttpSession session) {
        TDirtyword tDirtyword = new TDirtyword();
        tDirtyword.setDirtyword(dirtyword);
        Object user = session.getAttribute("user");
        if (user != null) {
            tDirtyword.setOwntype(((TAdminUser) user).getType());
            tDirtyword.setCreater(((TAdminUser) user).getId());
        }
        dirtywordService.addDirty(tDirtyword);
        return R.setOk();
    }

    @GetMapping("/all")
    public R selectAllDirtyByPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        PageInfo<TDirtyword> pageInfo = dirtywordService.selectAll(pageNum, pageSize);
        return R.setOk();
    }


    @PostMapping("dirty")
    public R updateDirtyById(@RequestBody TDirtyword tDirtyword, HttpSession session){
        Object user = session.getAttribute("user");
        if (user != null){
            Long id = ((TAdminUser) user).getId();
            Long type = ((TAdminUser) user).getType();
            tDirtyword.setCreater(id);
            tDirtyword.setOwntype(type);
        }
        dirtywordService.updateDirty(tDirtyword);
        return R.setOk();
    }

    @DeleteMapping("/dirty")
    public R deleteByIds(@RequestBody Map map){
        System.err.println(map);
        dirtywordService.deleteDirtyByIds((List<Integer>) map.get("ids"));
        return R.setOk();

    }

}
